

//
//  BAAlbumScrollCellNode.m
//  BabyAlbum
//
//  Created by master on 17/2/15.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BAAlbumScrollCellNode.h"

@interface BAAlbumScrollCellNode()
{
    ASDisplayNode *_bgNode;
    ASTextNode  *_titleNode;
    ASTextNode  *_detailNode;
    ASTextNode  *_dateNode;
    ASTextNode  *_weekNode;
    

}

@property (nonatomic, strong) ASButtonNode *playButton;

@end
//typedef ASLayoutSpec * (^ASLayoutSpecBlock)(__kindof ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize);

@implementation BAAlbumScrollCellNode

- (instancetype)initWithModel:(BAAlbumModel *)model {
    self = [super init];
    if(self) {

        self.backgroundColor = [UIColor clearColor];
//        self.cornerRadius = 15;
//        self.clipsToBounds = YES;
        
        _bgNode = [[ASDisplayNode alloc] init];
        _bgNode.cornerRadius = 15;
        _bgNode.clipsToBounds = NO;
        _bgNode.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
        _bgNode.shadowOffset = CGSizeMake(2, 10);
        _bgNode.shadowColor = RGBA_255(186, 221, 218,1).CGColor;
        _bgNode.shadowRadius = 8;
        _bgNode.shadowOpacity = 0.6;
        [self addSubnode:_bgNode];
        
        _imageNode = [[ASImageNode alloc] init];
        _imageNode.image = model.cover.coverImage?model.cover.coverImage:kDefaultCoverImage;
        _imageNode.cornerRadius = 5;
        _imageNode.clipsToBounds = YES;
        [self addSubnode:_imageNode];
        
        _playButton = [[ASButtonNode alloc] init];
        _playButton.hidden = model.cover.type == BAResourceTypeImage;
        _playButton.imageNode.contentMode = UIViewContentModeScaleAspectFit;
        [_playButton setImage:[UIImage imageNamed:@"play_btn"] forState:ASControlStateNormal];
        [_imageNode addSubnode:_playButton];
        
        
        _titleNode = [[ASTextNode alloc] init];
        NSAttributedString *string = [NSAttributedString attributedStringWithString:model.title fontSize:24 color:[UIColor blackColor]];
        _titleNode.attributedText = string;
        [self addSubnode:_titleNode];
        
        _dateNode = [[ASTextNode alloc] init];
        string =[NSAttributedString attributedStringWithString:[model customDateShow] fontSize:13 color:RGB_255(188,197,195)];
        _dateNode.attributedText = string;
        [self addSubnode:_dateNode];
        
        //TODO: 存在展示null的情况
        _weekNode = [[ASTextNode alloc] init];
        string = [NSAttributedString attributedStringWithString:[model weekDayShow] fontSize:13 color:RGB_255(188,197,195)];
        _weekNode.attributedText = string;
        [self addSubnode:_weekNode];

        _detailNode = [[ASTextNode alloc] init];
        
        string = [NSAttributedString attributedStringWithString:model.desc fontSize:13 color:RGBWhite(112.0)];
        _detailNode.attributedText = string;
        [self addSubnode:_detailNode];
     
        
 
        
        
    }
    return self;
}

- (void)didLoad {
    [super didLoad];
}

- (void)layout {
    [super layout];
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    
    _imageNode.style.preferredSize = CGSizeMake(constrainedSize.max.width - 30, constrainedSize.max.width - 30);
    _dateNode.style.preferredSize = CGSizeMake(constrainedSize.max.width - 80, 20);
//    _weekNode.style.preferredSize = CGSizeMake(constrainedSize.max.width / 2, 20);
    _playButton.style.preferredSize  = CGSizeMake(30, 30);
    _playButton.frame = CGRectMake(constrainedSize.max.width - 30 - 30- 10, constrainedSize.max.width - 30 - 30 - 10, 30, 30);

    _titleNode.style.flexShrink  = NO;
    _titleNode.style.maxSize = CGSizeMake(kScreenWidth - 30, 40);
    _detailNode.style.flexShrink = YES;
    _dateNode.style.flexShrink = NO;
   
    _detailNode.style.maxSize = CGSizeMake(constrainedSize.max.width - 10, constrainedSize.max.height - _imageNode.style.preferredSize.height - 18 - 5 - 13.5 - 5 - 40);
//    NSLog(@"%f",constrainedSize.max.height - _imageNode.style.preferredSize.height - 18 - 5 - 13.5 - 5 - 40);
//    _detailNode.style.maxSize = CGSizeMake(constrainedSize.max.width, constrainedSize.max.height - _imageNode.style.preferredSize.height - )
    ASRatioLayoutSpec *ratio = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1 child:_imageNode];
    
    ASStackLayoutSpec *ver1 = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[ratio,_titleNode]];
    
    ASRelativeLayoutSpec *dateSpec = [ASRelativeLayoutSpec relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionStart verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOption:ASRelativeLayoutSpecSizingOptionMinimumHeight child:_dateNode];
    ASRelativeLayoutSpec *weekSpec = [ASRelativeLayoutSpec relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionEnd verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOption:ASRelativeLayoutSpecSizingOptionMinimumHeight child:_weekNode];
    
    ASStackLayoutSpec *dataHor = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentEnd alignItems:ASStackLayoutAlignItemsCenter children:@[dateSpec,weekSpec]];

    ASStackLayoutSpec *ver2 = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[ver1,dataHor,_detailNode]];
    

    
    ASInsetLayoutSpec *insetLayout2 = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(15, 15, 35, 15) child:ver2];
    ASInsetLayoutSpec *bgInsetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(5, 5, 30, 5) child:_bgNode];
    ASBackgroundLayoutSpec *bg = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:bgInsetLayout background:insetLayout2];
    
    return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) child:bg];
}

@end
